﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Golos.Helper;
using Golos.Users.UserCache;

namespace Golos.Users.Http
{
    public partial class UserCacheServer
    {
        private void GetUserInfo(HttpListenerContext context, ref int statusCode)
        {
            try
            {
                if(context.Request.QueryString.AllKeys.Contains("userid"))
                {
                    Guid userId = new Guid(context.Request.QueryString["userid"]);
                    UserInfo userInfo = userCache.GetUserInfo(userId);
                    if(userInfo==null)
                    {
                        statusCode = (int)HttpStatusCode.NotFound;
                        return;
                    }
                    byte[] data = userInfo.Serialize();
                    context.Response.ContentLength64 = data.LongLength;
                    context.Response.ContentEncoding = Encoding.Default;
                    context.Response.OutputStream.Write(data, 0 ,data.Length);
                    statusCode = (int) HttpStatusCode.OK;
                }
            }
            catch (Exception exception)
            {
                LogClass.WriteErrorEntry(exception.ToString());
                statusCode = (int)HttpStatusCode.InternalServerError;
            }
        }
    }
}
